package com.bt.service.impl;

import com.aliyun.oss.common.utils.StringUtils;
import com.bt.dto.SysUserDTO;
import com.bt.entity.SysRole;
import com.bt.entity.SysUser;
import com.bt.mapper.SysRoleMapper;
import com.bt.mapper.SysUserMapper;
import com.bt.result.PageResult;
import com.bt.service.SysUserService;
import com.bt.vo.SysUserRVO;
import com.bt.vo.SysUserVO;
import com.bt.vo.SysUserp;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description TODO
 * @Author 锟
 * @Date 2023-05-02 21:06
 */
@Service
@Slf4j
public class SysUserServiceImpl implements SysUserService {
    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysRoleMapper sysRoleMapper;

    /**
     * 管理员管理分页查询
     *
     * @param page
     * @param limit
     * @param loginName
     * @param status
     * @param phone
     * @return
     */

    @Override
    public PageResult list(Integer page, Integer limit, String loginName, Integer status, String phone) {

        //参数设置
        PageHelper.startPage(page, limit);
        //执行返回查询列表
        List<SysUser> sysUserList = sysUserMapper.page(loginName, status, phone);
        List<String> collect = sysUserList.stream().map(s -> s.getRoleId().
                substring(2, s.getRoleId().length() - 2)).collect(Collectors.toList());
        List<SysRole> s = sysRoleMapper.selebyid(collect);

        //数据列表转成Page<SysUser>
        Page<SysUser> pagePut = (Page<SysUser>) sysUserList;

        List<SysUserVO> collect1 = pagePut.stream().map(s1 -> new SysUserVO(s1.getId(), s1.getLoginName(),
                s1.getPassword(), s1.getName(), s1.getEmail(), s1.getPhone(),
                s1.getLoginIp(), s1.getLoginDate(), s1.getCreateDate(), s1.getUpdateDate(),
                s1.getDelFlag(), s1.getRoleId(), s1.getStatus(), null)).collect(Collectors.toList());
        for (SysRole r : s) {
            for (SysUserVO userVO : collect1) {
                if (userVO.getRoleId().substring(2, userVO.getRoleId().length() - 2).equals(r.getId())) {
                    userVO.setRolename(r.getName());
                }
            }
        }
        //封装返回
        return new PageResult(pagePut.getTotal(), pagePut.getPages(),
                pagePut.getPageSize(), pagePut.getPageNum(), collect1);
    }


    /**
     * 管理员删除
     *
     * @param id
     */
    @Override
    public void detele(String id) {
        //删除管理员信息
        sysUserMapper.deteleById(id);
    }


    /**
     * 新增分类
     *
     * @param sysUserDTO
     */
    @Override
    @Transactional
    public void saveWithRole(SysUserDTO sysUserDTO) {
        SysUser sysUser = new SysUser();
        //赋值
        BeanUtils.copyProperties(sysUserDTO, sysUser);

        //通过UUID生成随机ID码
        sysUser.setId(UUID.randomUUID().toString());
        //生成创建日期
        sysUser.setCreateDate(new Date());

//        sysUser.setDel_flag(DefaultDateConstant.DISDELE);
//        sysUser.setDefault_data(DefaultDateConstant.NO);

        List<String> roleId = Collections.singletonList(sysUserDTO.getRole_id());
        String roleid = StringUtils.join(",", roleId);
        String role_id = "["+"\"" + roleid+"\""+"]";
        sysUser.setRoleId(role_id);
        sysUserMapper.add(sysUser);
    }



    /**
     * 根据id查询管理员回显接口
     *
     * @param id
     * @return
     */

    @Override
    public SysUser findId(String id) {
        return sysUserMapper.findById(id);

    }

//        return sysUserMapper.findById(id);


/*        log.info("管理员id为：{}", id);
        SysUser sysUser = sysUserMapper.findId(id);

        List<String> roleId = sysUserMapper.findRoleId();

//        for (String s : roleId) {
//            SysRole sysRole = sysRoleMapper.findById(s);
//        }
        SysUserRVO sysUserRVO = new SysUserRVO();
        sysUserRVO.setSysUser(sysUser);
        sysUserRVO.setRole_id(roleId);

        return sysUserRVO;
    }*/

        /*
        SysUser id1 = sysUserMapper.findId(id);
        System.out.println(id1);
        SysUserp sysUserp = new SysUserp();
        sysUserp.setId(id1.getId());
        sysUserp.setPassword(id1.getPassword());
        sysUserp.setName(id1.getName());
        sysUserp.setEmail(id1.getEmail());
        sysUserp.setPhone(id1.getPhone());
        sysUserp.setRoleId(id1.getRoleId());
        sysUserp.setStatus(id1.getStatus());
        sysUserp.setLogin_name(id1.getLoginName());
        sysUserp.setLogin_ip(id1.getLoginIp());
        sysUserp.setLogin_date(id1.getLoginDate());
        sysUserp.setCreate_date(id1.getCreateDate());
        sysUserp.setUpdate_date(id1.getUpdateDate());
        sysUserp.setDel_flag(id1.getDelFlag());
        sysUserp.setDefault_data(id1.getDefaultData());
        String substring = id1.getRoleId().substring(2, id1.getRoleId().length() - 2);
        List<String> rooid = new ArrayList<>();
        rooid.add(substring);
        sysUserp.setRole_id(rooid);
        return sysUserp;*/


/*        @Override
        public SysUserVO findId(String id) {
        SysUser sysUser = sysUserMapper.findById(id);
        //实例对象
        SysUserVO sysUserVO = new SysUserVO();
        //赋值
        BeanUtils.copyProperties(sysUser,sysUserVO);

        sysUserVO.setRoleId(sysUser.getRoleId());
        String[] slice = slice(sysUserVO.getRoleId());
        List<String> role_id = new ArrayList<>();
        for (int i = 0;i < slice.length; i++){
            role_id.add(slice[0]);
        }
    }*/

/*    public String[] slice(String str){
        String substring = str.substring(2, str.length() - 2);
        String[] split = substring.split(",");
        return split;
    }
*/


    /**
     * 修改员工信息
     */
    @Override
    public void update(SysUserDTO sysUserDTO) {

        //实例管理员对象
        SysUser sysUser = new SysUser();
        BeanUtils.copyProperties(sysUserDTO, sysUser);

        //调用mapper修改
        sysUserMapper.update(sysUser);
    }

}
